home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SNNSV32.ZIP / SNNSv3.2 / tools / sources / netperf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-25  |  6.7 KB  |  234 lines

  1. /**********************************************************************
  2. FILE   : netperf.c
  3. PURPOSE: SNNS-Kernel Performance Test Program
  4. NOTES  : 
  5. AUTHOR : Niels Mache
  6. DATE   : 30.07.90
  7. VERSION : 1.4  3/24/93
  8.  
  9.              Copyright (c) 1990-1993  SNNS Group, IPVR, Univ. Stuttgart, FRG
  10.  
  11. **********************************************************************/
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include <time.h>
  18. #ifndef  __BORLANDC__
  19. #include <sys/param.h>
  20. #include <sys/types.h>
  21. #include <sys/times.h>
  22. #include <sys/utsname.h>
  23. #endif
  24.  
  25.  
  26. /*  SNNS-Kernel constants and data type definitions  */
  27. #include "glob_typ.h"
  28. /*  SNNS-Kernel User-Interface Function Prototypes   */
  29. #include "kr_ui.h"
  30.  
  31.  
  32.  
  33. static void  errChk( err_code )
  34. int  err_code;
  35. {
  36.   if (err_code != KRERR_NO_ERROR)  {
  37.     fprintf( stderr, "%s\n", krui_error( err_code ));
  38.     exit( 1 );
  39.   }
  40. }
  41.  
  42.  
  43. int main()
  44. {
  45.   int    ret_code, N, i, j, no_of_sites, no_of_links, no_of_units,
  46.     no_of_patterns, dummy, step, NoOfReturnVals, no_of_input_params,
  47.     no_of_output_params, choose_me,
  48.           SubPatternISize_param[5],
  49.         SubPatternOSize_param[5],
  50.         SubPatternIStep_param[5],
  51.         SubPatternOStep_param[5];
  52.  
  53.   double  cpu_time, perf;
  54.   char    *netname, file_name[80];
  55.   float learn_parameters[5];
  56.   float *return_values;
  57. #ifdef  __BORLANDC__
  58.   clock_t  init_time;
  59. #else
  60.   struct tms  tp;
  61.   long  HZ_value;
  62.   long init_time;
  63. #endif
  64.  
  65.  
  66. #ifndef  __BORLANDC__
  67.   /*  get HZ value  */
  68.   if ((getenv( "HZ" ) == NULL) ||
  69.       (sscanf( getenv( "HZ" ), "%d", &HZ_value ) != 1))  {
  70. #ifndef HZ
  71.     fprintf( stderr, "****  WARNING: No HZ defined!\n" );
  72.     HZ_value = 60;
  73. #else
  74.     HZ_value = HZ;
  75. #endif
  76.   }
  77. #endif
  78.  
  79.  
  80.   fprintf( stdout, "\n%s\n", krui_getVersion() );
  81.   fprintf( stdout, "-----  Benchmark Test  -----\n\n" );
  82.  
  83.   fprintf( stdout,"Filename of the network file: ");
  84.   scanf("%s", file_name);
  85.  
  86.   fprintf( stdout, "Loading the network ...\n\n" );
  87.   ret_code = krui_loadNet( file_name, &netname );
  88.   errChk( ret_code );
  89.   krui_getNetInfo( &no_of_sites, &no_of_links, &dummy, &dummy );
  90.   no_of_units = krui_getNoOfUnits();
  91.   fprintf( stdout, "Network name: %s\n", netname );
  92.   fprintf( stdout, "No. of units       : %d\n", no_of_units );
  93.   fprintf( stdout, "No. of input units : %d\n", krui_getNoOfTTypeUnits( INPUT ) );
  94.   fprintf( stdout, "No. of output units: %d\n", krui_getNoOfTTypeUnits( OUTPUT ) );
  95.   fprintf( stdout, "No. of sites: %d\n", no_of_sites );
  96.   fprintf( stdout, "No. of links: %d\n\n", no_of_links );
  97.   fprintf( stdout, "Learning function: %s\n", krui_getLearnFunc() );
  98.   fprintf( stdout, "Update function  : %s\n", krui_getUpdateFunc() );
  99.  
  100.   fprintf( stdout, "\nFilename of the pattern file: " );
  101.   scanf( "%s", file_name );
  102.   fprintf( stdout, "Loading the patterns ...\n\n" );
  103.  
  104. #ifdef  OLDFORMAT
  105.   ret_code = krui_loadPatterns( file_name );
  106. #else
  107.   ret_code = krui_loadNewPatterns( file_name, &dummy );
  108. #endif
  109.   errChk( ret_code );
  110.   no_of_patterns = krui_getNoOfPatterns();
  111.   fprintf( stdout, "No. of patterns: %d\n", no_of_patterns );
  112.  
  113.  
  114.   errChk(krui_DefTrainSubPat(SubPatternISize_param,SubPatternOSize_param,
  115.                      SubPatternIStep_param,SubPatternOStep_param,
  116.                      &dummy));
  117.  
  118.   fprintf( stdout,"\nDo you want to benchmark\nPropagation [1] or\nBackpropagation [2] ?\nInput: ");
  119.   scanf( "%d", &choose_me );
  120.  
  121.  
  122. #if defined(VECTOR_KERNEL)
  123.   fprintf( stdout,"\nVectorize Network? (y/n): " );
  124.   scanf("%s", file_name);
  125.  
  126.   if (file_name[0] == 'y')  {
  127.     fprintf( stdout,"Vectorize Network...\n" );
  128.     errChk( krui_setSpecialNetworkType(KERNEL_MODE_VECTOR) );
  129.     fprintf( stdout,"Network vectorized.\n" );
  130.   }
  131. #endif
  132. /*
  133. #if defined(VECTOR_KERNEL)
  134.   fprintf( stdout,"Devectorize Network...\n" );
  135.   errChk( krui_setSpecialNetworkType(KERNEL_MODE_STD) );
  136.   fprintf( stdout,"Network devectorized.\n" );
  137. #endif
  138. */
  139.  
  140.   fprintf( stdout, "\nChoose no. of cycles: " );
  141.   scanf("%d", &N);
  142.   if (N <= 0)  {
  143.      fprintf( stdout, "\nInvalid no. of cycles !\n" );
  144.      exit( 1 );
  145.   }
  146.   if (choose_me == 1)  {
  147.     fprintf( stdout, "\nBegin propagation ...\n" );
  148.  
  149. #ifdef  __BORLANDC__
  150.     init_time = clock();
  151. #else
  152.     (void) times( &tp );
  153.     init_time = tp.tms_utime;
  154. #endif
  155.  
  156.     for (i = 0; i < N; i++)  {
  157.       for (j=1; j <= no_of_patterns; j++)  {
  158. /*
  159.     krui_setPatternNo(j);
  160.         errChk( ret_code );
  161.         krui_showPattern(OUTPUT_NOTHING);
  162.         errChk( ret_code );
  163. */
  164.         ret_code = krui_updateNet( learn_parameters, 0 );
  165.         errChk( ret_code );
  166.       }
  167.     }
  168.   }
  169.   else  {
  170.     /*  determine the no. of parameters of the current learning function
  171.     */
  172.     (void) krui_getFuncParamInfo( krui_getLearnFunc(), LEARN_FUNC,
  173.                   &no_of_input_params, &no_of_output_params );
  174.     fprintf( stdout, "\nThe learning function '%s' needs %d input parameters:\n",
  175.       krui_getLearnFunc(), no_of_input_params );
  176.     for (i = 0; i < no_of_input_params; i++)  {
  177.        fprintf( stdout, "Parameter [%d]: ", i + 1 );
  178.        scanf( "%f", &learn_parameters[ i ] );
  179.     }
  180.  
  181.     step = ((N - 1) / 10) + 1;
  182.     fprintf( stdout, "\nBegin learning ...\n" );
  183.  
  184. #ifdef  __BORLANDC__
  185.     init_time = clock();
  186. #else
  187.     (void) times( &tp );
  188.     init_time = tp.tms_utime;
  189. #endif
  190.  
  191.     for (i = 0; i < N; i++)  {
  192. /*  REMEMBER:  return_values[ 0 ] returns the current net error
  193.                learn_parameters[ 0 ] contains the learning parameter (backpropagation)
  194. */
  195.       ret_code = krui_learnAllPatterns( learn_parameters, 1, &return_values, &NoOfReturnVals );
  196.       errChk( ret_code );
  197.  
  198.       /*  print the return values of the learning function  */
  199.       if ((i % step == 0) || i == (N - 1))  {
  200.         fprintf( stdout, "\nCycle: %d\nLearning function value(s): ", i + 1 );
  201.         for (j = 0; j < NoOfReturnVals; j++ )
  202.       fprintf( stdout, "[%d]: %f  ", j + 1, return_values[ j ] );
  203.         fprintf( stdout, "\n" );
  204.       }
  205.     }
  206.   }
  207.  
  208.  
  209. #ifdef  __BORLANDC__
  210.   cpu_time = (double) (clock() - init_time) / (double) CLK_TCK;
  211. #else
  212.   (void) times( &tp );
  213.   cpu_time = (double) (tp.tms_utime - init_time) / (double) HZ_value;
  214. #endif
  215.  
  216.   fprintf( stdout, "\n\nNo. of units updated: %.0f\n", (double) no_of_units * (double) N * (double) no_of_patterns );
  217.   fprintf( stdout, "No. of sites updated: %.0f\n", (double) no_of_sites * (double) N * (double) no_of_patterns);
  218.   fprintf( stdout, "No. of links updated: %.0f\n", (double) no_of_links * (double) N * (double) no_of_patterns);
  219.   fprintf( stdout, "CPU Time used: %.2f seconds\n", cpu_time );
  220.  
  221.   perf = (double) no_of_links * (double) N * (double) no_of_patterns / cpu_time;
  222.   if (choose_me == 1)  {
  223.     fprintf( stdout, "\nNo. of connection per second (CPS): %.4e\n", perf );
  224.   }
  225.   else  {
  226.     fprintf( stdout, "\nNo. of connection updates per second (CUPS or WUPS): %.4e\n", perf );
  227.   }
  228.  
  229.   /*  before exiting: delete network  */
  230.   krui_deleteNet();
  231.  
  232.   return( 0 );
  233. }
  234.